package com.ruoyi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.entity.Category;
import com.ruoyi.common.entity.dto.CategoryDTO;
import com.ruoyi.common.entity.dto.SelectCategoryDto;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.service.IWebCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;


import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * @author 0617
 * @version 1.0
 * @package:com.ruoyi.controller
 * @date 2025/8/17 下午8:40
 * @description: TODO
 */
@RestController
@RequestMapping("/cates/cate")
public class WebCateController extends BaseController {

    @Autowired
    private IWebCategoryService categoryService;
    @GetMapping("/getCategory")
    public R<List<SelectCategoryDto>> getCategory() {
        List<SelectCategoryDto> categoryDtos = categoryService.getCategory();
        return R.ok(categoryDtos);
    }


    /**
     * 查询项目分类列表
     */
    @PreAuthorize("@ss.hasPermi('cates:cate:list')")
    @GetMapping("/list")
    public TableDataInfo list(Category category)
    {
        startPage();
        List<Category> list = categoryService.selectCategoryList(category);
        return getDataTable(list);
    }

    /**
     * 导出项目分类列表
     */
    @PreAuthorize("@ss.hasPermi('cates:cate:export')")
    @Log(title = "项目分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Category category) {
        List<Category> list = categoryService.selectCategoryList(category);
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        util.exportExcel(response, list, "项目分类数据");
    }

    /**
     * 获取项目分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('cates:cate:query')")
    @GetMapping(value = "/{id}")
    public R<Category> getCategoryById( @PathVariable Integer id) {
        return categoryService.getCategoryById(id);
    }

    /**
     * 新增项目分类
     */
    @PreAuthorize("@ss.hasPermi('cates:cate:add')")
    @Log(title = "项目分类", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Category> createCategory(@Valid @RequestBody CategoryDTO categoryDTO) {
        return categoryService.createCategory(categoryDTO);
    }


    /**
     * 修改项目分类
     */
    @PreAuthorize("@ss.hasPermi('cates:cate:edit')")
    @Log(title = "项目分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody Category category) {
        CategoryDTO categoryDTO = new CategoryDTO();
        BeanUtils.copyProperties(category, categoryDTO);
        R<Category> categoryR = categoryService.updateCategory(category.getId(), categoryDTO);

        return R.ok();
    }

    /**
     * 删除项目分类
     */
    @PreAuthorize("@ss.hasPermi('cates:cate:remove')")
    @Log(title = "项目分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(categoryService.deleteCategoryByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('cates:cate:edit')")
    @Log(title = "项目分类", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public R updateStatus(@RequestBody Category category) {
        CategoryDTO categoryDTO = new CategoryDTO();
        BeanUtils.copyProperties(category, categoryDTO);
        R<Category> categoryR = categoryService.updateCategory(category.getId(), categoryDTO);

        return categoryR;
    }
}
